<?php

namespace app\service;

use app\BaseController;
use app\controller\Image;
use app\model\SystemDynamicFromField;
use think\App;

/**
 * 自定义字段处理类
 */
class CustomField extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct ($app);
    }

    /**
     * 添加编辑
     * @param $data
     * @return false|string
     */
    public function addAndEditCustomFieldHandle($data)
    {
        $systemFieldModel = new SystemDynamicFromField();
        $image = new Image($this->app);
        if (isJsonString($data)) {
            $data = json_decode($data, true);
        }
        //$data 为空返回空json串
        if (!$data) return json_encode($data);
        //data 中数据格式为 自定义字段ID => 值
        $field_ids = array_keys($data);
        //多文件上传自定义字段ID数组
        $file_field_ids = $systemFieldModel
            ->where('id', 'in', $field_ids)
            ->where('type', 'file')
            ->column('id');
        foreach ($data as $key => &$datum) {
            if (in_array($key, $file_field_ids)) {
                //多文件上传
                $data[$key] = $image->removeUrl($datum);
            }
        }
        return json_encode($data);
    }


    public function listCustomFieldHandle($data)
    {
        $systemFieldModel = new SystemDynamicFromField();
        $image = new Image($this->app);
        $array = $data;
        $data = $data['custom'];
        if (is_object($data)) {
            $data = (array)$data;
        }elseif(isJsonString($data)){
            $data = json_decode($data,true);
        }
        //$data 为空返回空json串
        if (!$data) return $array;
        //data 中数据格式为 自定义字段ID => 值
        $field_ids = array_keys($data);
        //多文件上传自定义字段ID数组
        $field_list = $systemFieldModel
            ->where('id', 'in', $field_ids)
            ->select()
            ->toArray();
        $field_array = [];
        $file_field_ids = [];

        foreach($field_list as $v) {
            if($v){
                $field_array[$v['id']] = $v['alias_field_id'];
                if($v['type'] === 'file'){
                    $file_field_ids[] = $v['id'];
                }
            }
        }
        foreach ($data as $key => &$datum) {
            if(!empty($field_array)){
                if (in_array($key, $file_field_ids)) {
                    //多文件上传
                    $array[$field_array[$key]] = $image->spliceFileName($datum);
                }else{
                    if(array_key_exists($key,$field_array)){
                        $array[$field_array[$key]] = $datum;
                    }
                }
            }
        }
        $array['custom'] = $data;
        return $array;
    }


    public function detailsCustomFieldHandle($data)
    {
        $systemFieldModel = new SystemDynamicFromField();
        $image = new Image($this->app);
        if (isJsonString($data)) {
            $data = json_decode($data, true);
        }
        //$data 为空返回空json串
        if (!$data) return $data;
        //data 中数据格式为 自定义字段ID => 值
        $field_ids = array_keys($data);
        //多文件上传自定义字段ID数组
        $file_field_ids = $systemFieldModel
            ->where('id', 'in', $field_ids)
            ->where('type', 'file')
            ->column('id');
        foreach ($data as $key => &$datum) {
            if (in_array($key, $file_field_ids)) {
                //多文件上传
                $data[$key] = $image->spliceUrl($datum);
            }
        }
        return $data;
    }
}